const db = require('../db/index');

exports.getReviewMessage=(req,res)=>{
  const sql = 'SELECT * FROM reviews';
  db.query(sql, (err, results) => {
    if (err) {
      return res.status(500).send('Database error');
    }
    res.status(200).json({ code:0, message: '评价获取成功', data: results });
  });
}

exports.getReviewImg = (req, res) => {
  const order_id = req.params.order_id
  const sql = `select * from reviews_img where order_id=?`
  db.query(sql, order_id, (err, results) => {
    if (err) {
      return res.status(500).json({ message: '评价表图片获取失败' })
    }
    res.json({ code:0, message: '评价表图片获取成功', data: results });
  })
}

exports.getComment = (req, res) => {
  const order_id = req.params.order_id
  const query = `select * FROM comments where order_id=?`
  db.query(query, order_id, (err, results) => {
    if (err) {
      console.error('查询错误:', err);
      return res.status(500).json({ message: '查询失败' });
    }
    res.json({ code:0, message: '评论信息获取成功', data: results });  // 返回所有有效的优惠券
  })

}
exports.delReviewMessage=(req,res)=>{
  const order_id = req.params.order_id
  const sql = 'DELETE FROM reviews WHERE order_id=?';
  db.query(sql, order_id,(err, results) => {
    if (err) {
      return res.status(500).send('Database error');
    }
    res.status(200).json({ code:0 });
  });
}

exports.delReviewImg = (req, res) => {
  const order_id = req.params.order_id
  const sql = `DELETE FROM reviews_img WHERE order_id=?`
  db.query(sql, order_id, (err, results) => {
    if (err) {
      return res.status(500).send('Database error');
    }
    res.json({ code:0});
  })
}

exports.delComment = (req, res) => {
  const order_id = req.params.order_id
  const sql = `DELETE FROM comments WHERE order_id=?`
  db.query(sql, order_id, (err, results) => {
    if (err) {
      return res.status(500).send('Database error');
    }
    res.json({ code:0});  // 返回所有有效的优惠券
  })

}